<?php
header('Content-Type: application/json');
require_once '../api/mentals.php';

try {
    // 处理获取类型请求
    if (isset($_GET["action"]) && $_GET["action"] === "getTypes") {
        getMentalsType();
        exit;
    }

    $currentPage = isset($_GET["currentPage"]) ? intval($_GET["currentPage"]) : 1;
    $pageSize = isset($_GET["pageSize"]) ? intval($_GET["pageSize"]) : 10;
    $typeId = isset($_GET["typeId"]) ? intval($_GET["typeId"]) : 0;
    $mentalId = isset($_GET["mentalId"]) ? intval($_GET["mentalId"]) : 0;

    // 处理搜索请求
    if (isset($_GET["searchStr"]) && !empty($_GET["searchStr"])) {
        $searchStr = $_GET["searchStr"];
        searchMentalsByPage($searchStr, $currentPage, $pageSize);
    }
    // 处理类型分页请求 
    else if(isset($typeId) && !empty($typeId)){
        getMentalsByType($typeId, $currentPage, $pageSize);
    }
    // 处理文章详情请求
    else if(isset($mentalId) && !empty($mentalId)){
        getMentalById($mentalId);
    }
    // 处理普通分页请求
    else {
        getMentalsByPage($currentPage, $pageSize);
    }

} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => '服务器错误',
        'data' => null,
        'errors' => $e->getMessage()
    ]);
}

